#include"BlockQueue.hpp"
#include"task.hpp"
#include<time.h>

int myadd(int x,int y)
{
    return x+y;
}
void* consumer(void* args)
{
    BlockQueue<Task> * bqueue=(BlockQueue<Task>*)(args);
    while(1)
    {
        //获取任务
        Task t;
        bqueue->pop(&t);
        //完成任务
        std::cout<<"comsumer:->"<<t._x<<"+"<<t._y<<"="<<t()<<std::endl;
        sleep(1);
    }

    return nullptr;
}

void* productor(void* args)
{
    BlockQueue<Task> * bqueue=(BlockQueue<Task>*)(args); 
    while(1)
    {
        //任务派发
        int x,y;
        // int x=rand()%10+1;  
        // usleep(rand()%1000);
        // int y=rand()%5+1;
        std::cout<<"please enter x:->"<<std::endl;
        std::cin>>x;
        std::cout<<"please enter x:->"<<std::endl;
        std::cin>>x;
        Task t(x,y,myadd);
        //生产任务  
        bqueue->push(t);
        //输出信息
        std::cout<<"productor->"<<t._x<<"+"<<t._y<<"=?"<<std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    srand((unsigned int )time(nullptr)^getpid()^0x2003);
    BlockQueue<Task> *bqueue=new BlockQueue<Task>(5);
    pthread_t c,p;
    pthread_create(&p,nullptr,productor,(void*) bqueue);
    pthread_create(&c,nullptr,consumer,(void*)bqueue);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);
    delete bqueue;
}